/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package vip.web3.api.service.impl.base;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import vip.web3.api.service.base.ApiFileUploadService;
import vip.web3.common.utils.Prop;
import vip.web3.common.utils.R;
import vip.web3.common.utils.UploadUtils;

@Service("apiFileUploadService")
public class ApiFileUploadServiceImpl implements ApiFileUploadService {

    @Autowired
    private UploadUtils uploadUtils;

    @Autowired
    private Prop prop;

    @Override
    public R uploadBase64(String file) {
        return uploadUtils.uploadBase64(file, prop.getRootPath(), prop.getFileUploadDir(), ".png", prop.getIsOss());
    }

    @Override
    public R uploadFile(MultipartFile file) {
        try{
            String subFix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            return uploadUtils.uploadFile(file.getBytes(), prop.getRootPath(), prop.getFileUploadDir(), subFix, prop.getIsOss());
        } catch (Exception e){
            return R.error("文件上传失败");
        }
    }
}
